/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.niedziela12.pai.sessionbeans;

import com.niedziela12.pai.ejb.dto.UserRolesDTO;
import com.niedziela12.pai.ejb.user.endpoints.UserEndpoint;
import com.niedziela12.pai.ejb.user.endpoints.UserEndpointLocal;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Kamil
 */
@ManagedBean
@SessionScoped
public class UserRights implements Serializable{

    @EJB
    private UserEndpointLocal userEndpoint;
    /**
     * Creates a new instance of UserRights
     */
    public UserRights() {
    }
    
    public boolean isAdmin(){
        if(null != FacesContext.getCurrentInstance().getExternalContext().getRemoteUser())
            for(UserRolesDTO ur : userEndpoint.getUsersRoles(userEndpoint.getUserByLogin(
                    FacesContext.getCurrentInstance().getExternalContext().getRemoteUser()))){
                if("admin".equals(ur.getIdRole().getName()) && ur.isActive())
                    return true;
            }
        return false;
    }
    
    public boolean isModerator(){
        if(null != FacesContext.getCurrentInstance().getExternalContext().getRemoteUser())
            for(UserRolesDTO ur : userEndpoint.getUsersRoles(userEndpoint.getUserByLogin(
                    FacesContext.getCurrentInstance().getExternalContext().getRemoteUser()))){
                if("moderator".equals(ur.getIdRole().getName()) && ur.isActive())
                    return true;
            }
        return false;
    }
    
    public boolean isClient(){
        if(null != FacesContext.getCurrentInstance().getExternalContext().getRemoteUser())
            for(UserRolesDTO ur : userEndpoint.getUsersRoles(userEndpoint.getUserByLogin(
                    FacesContext.getCurrentInstance().getExternalContext().getRemoteUser()))){
                if("client".equals(ur.getIdRole().getName()) && ur.isActive())
                    return true;
            }
        return false;
    }
}
